package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisMessageConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Reference
    private OrderSettingService orderSettingService;

    @Reference
    private MemberService memberService;

    @Override
    @LcnTransaction
    public Order add(Map map) {
        //1.校验验证码
        String formCode = (String) map.get("validateCode");
        String telephone = (String) map.get("telephone");
        String redisCode = RedisUtil.get(RedisMessageConstant.SENDTYPE_ORDER + telephone);
        //表单是否为空
        if (StringUtils.isBlank(formCode)) {
            throw new RuntimeException("验证码必须填写");
        }
        //是否过期
        if (StringUtils.isBlank(redisCode)) {
            throw new RuntimeException("验证码已过期");
        }
        //是否相等
        if (!redisCode.equals(formCode)) {
            throw new RuntimeException("验证码错误");
        }
        //2.判断是否预约满
        String orderDate = (String) map.get("orderDate");
        System.out.println("orderDate = " + orderDate);
        int count = orderSettingService.isOrderOk(orderDate);
        if (count < 0) {
            throw new RuntimeException("今日预约已满，请选择其他日期");
        }
        String setmealId =(String)map.get("setmealId");

        //3.判断用户是否重复预约
        Member member = memberService.findMemeberByTelephone(telephone);
        if (member != null) {
            int orderExist = baseMapper.haveOrderOrNot(member.getId(), Integer.parseInt(setmealId), orderDate);
            if (orderExist > 0) {
                throw new RuntimeException("您已预约，请不要重复预约");
            }
        }else{
            //更新会员表
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date(System.currentTimeMillis()));
            member.setSex((String) map.get("sex"));
            member.setIdCard((String)map.get("idCard"));
            memberService.save(member);
        }
        //4.可以预约，多表更新
        orderSettingService.updateReservetionsByOrderDate(orderDate);


        //  订单表的录入
        Order  order = new  Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate,"yyyy-MM-dd"));
        order.setOrderType((String)map.get("orderType"));
        order.setSetmealId(Integer.parseInt(setmealId));
        order.setOrderStatus("未到诊");
        save(order);
        return order;
    }

    @Override
    public Map findById(int id) {
        Map map =  baseMapper.findById(id);
        System.out.println("orderserviceMap = " + map);
        return map;
    }

}
